import 'package:flutter/services.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:flutter/material.dart';

//输入文本框
class InputWidget extends StatefulWidget {
  double width;
  double height;
  String hitText;
  int numlines;
  int maxlength;
  bool obscureText;
  TextAlign textAlignment;
  final Function finishCallback;
  Color borderColor;
  Color selectBorderColor;
  double fontSize;
  TextInputType keyBoardtype;
  InputWidget(this.hitText,{this.fontSize =15,this.keyBoardtype=TextInputType.text, this.obscureText, this.selectBorderColor =Colors.white,this.borderColor =Colors.white, this.textAlignment =TextAlign.left,this.maxlength=11, this.finishCallback, this.numlines=1, this.width,this.height=50,Key key}) : super(key: key);
  @override
  _InputWidgetState createState() => _InputWidgetState();
}

class _InputWidgetState extends State<InputWidget> {

  TextEditingController _contentController = new TextEditingController();
  FocusNode _contentFocusNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    return Container(
      height:ScreenUtil().setWidth(widget.height),
      width: ScreenUtil().setWidth(widget.width??200),
      padding: EdgeInsets.only(top: ScreenUtil().setWidth(5)),
      child: TextField(
        focusNode: _contentFocusNode,
        obscureText: widget.obscureText??false,
        controller: _contentController,
        maxLines:widget.numlines>1?widget.numlines:1, //不限制行数
        autofocus: false,
        textDirection:TextDirection.ltr,
          keyboardType:widget.keyBoardtype??TextInputType.number,
          textCapitalization:TextCapitalization.characters,
        decoration: InputDecoration(
          border:OutlineInputBorder(borderSide: BorderSide.none),
          hintText: widget.hitText,
            // 主要添加以下代码
          enabledBorder: new UnderlineInputBorder( // 不是焦点的时候颜色
              borderSide: BorderSide(
                  color:widget.borderColor
              ),
            ),
            focusedBorder: new UnderlineInputBorder( // 焦点集中的时候颜色
              borderSide: BorderSide(
                  color:widget.selectBorderColor
              ),
            ),
        ),

        style: TextStyle(fontFamily: "PingFangSC-Regular", color: Colors.black,fontSize: ScreenUtil().setSp(widget.fontSize),height: 1.1),textAlign: widget.textAlignment,
        //文本内容改变时回调
        onChanged: (text) {
          print('文本内容改变时回调 $text');
          // chooseCallback(text);
          if(text.length > widget.maxlength){
             text = text.substring(0,widget.maxlength);
          }
           widget.finishCallback(text);
          _contentController.selection = TextSelection.fromPosition(
              TextPosition(offset: _contentController.text.length));
        },
        //内容提交时回调
        onSubmitted: (text) {
          print('内容提交时回调 $text');
          FocusScope.of(context).requestFocus(FocusNode());
        },
      ),
    );;
  }
}

